<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 步骤  -->
    <el-steps class="step" :active="addObj.active * 1" finish-status="success">
      <el-step title="基本信息"></el-step>
      <el-step title="商品参数"></el-step>
      <el-step title="商品属性"></el-step>
      <el-step title="商品图片"></el-step>
      <el-step title="商品内容"></el-step>
    </el-steps>

    <!-- 对应步骤的名称 -->
    <el-tabs @tab-click="Changepage"  @change="change" v-model="addObj.active" tab-position="left" >
      <!-- 基本信息表单 -->
      <el-tab-pane  name="0" label="基本信息">

        <template v-if="addObj.active == '0'">
          <el-form :model="addObj.editAppInfoFrom" style="height:500px; ">
              <el-form-item style="margin-bottom:52px;" label="商品名称"
                  prop="goods_name"
                  :rules="[ { required: true, message: '请输入商品名称', trigger: 'blur' }]">
                  <el-input v-model="addObj.editAppInfoFrom.goods_name" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item style="margin-bottom:52px;" label="商品价格"
                  prop="goods_price"
                  :rules="[ { required: true, message: '请输入商品价格', trigger: 'blur' }]">
                  <el-input v-model="addObj.editAppInfoFrom.goods_price" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item style="margin-bottom:52px;" label="商品重量"
                  prop="goods_weight"
                  :rules="[ { required: true, message: '请输入商品重量', trigger: 'blur' }]">
                  <el-input v-model="addObj.editAppInfoFrom.goods_weight" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item style="margin-bottom:52px;" label="商品数量"
                  prop="goods_number"
                  :rules="[ { required: true, message: '请输入商品数量', trigger: 'blur' }]">
                  <el-input v-model="addObj.editAppInfoFrom.goods_number" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="商品分类">
                  <el-cascader
                      v-model="addObj.value"
                      :props="addObj.options"
                      :options="addObj.list"
                      @change="handleChange"
                      @click="deliverAppDialogFn">
                  </el-cascader>
              </el-form-item>
          </el-form>
        </template>

      </el-tab-pane>

      <el-tab-pane name="1" label="商品参数">
        <template v-if="addObj.active == '1'"> 

             <div v-for="(item) in addObj.dongList" :key="item.attr_id">
                <p class="boxx">{{item.attr_name}}</p>
                <el-checkbox-group v-model="item.attr_vals">
                    <el-checkbox border v-for="(item1,index) in item.attr_vals" 
                    :key="index" :label="item1"></el-checkbox>
                </el-checkbox-group>

            </div>

        </template>
      </el-tab-pane>
      <el-tab-pane name="2" label="商品属性">
        <template v-if="addObj.active == '2'"> 

            <el-form ref="form">
                <el-form-item v-for="item in addObj.jingList" :key="item.attr_id" :label="item.attr_name">
                    <el-input class="inputk" v-model="item.attr_vals"></el-input>
                </el-form-item>
            </el-form>

        </template>
      </el-tab-pane>
      <el-tab-pane name="3" label="商品图片">
        <template v-if="addObj.active == '3'"> 

          <!-- <el-upload
              :action="addGoods.URL"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :file-list="fileList"
              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <template #tip>
                <div class="el-upload__tip">
                  jpg/png files with a size less than 500kb
                </div>
              </template>
          </el-upload> -->

        </template>
      </el-tab-pane>
      <el-tab-pane name="4" label="商品内容">
        <template v-if="addObj.active == '4'"> 

          <el-button @click="edieAdd" type="primary">添加商品</el-button>

        </template>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import { ref, reactive, watch ,computed} from "vue";
import { useRouter, useRoute } from "vue-router";
import { getCategories, getActiveList, addGoods } from "../../http/api.js";
import {ElMessage} from "element-plus"
// import {baseURL} from "../../http/index.js"
// import {useStore} from "vuex"
//getCategories 获取商品分类的方法
//getActiveList 获取参数列表
//addGoods 添加商品
export default {
  setup() {
    //定义数据

    // let active = ref(0);
    const router=useRouter()
    // const store=useStore()


    let addObj = reactive({//声明响应式数据



      active:0,

      list:[],

      value: [],

      editAppInfoFrom: {
        goods_name: "", //名称
        goods_price: "", //价格
        goods_number: "", //数量
        goods_weight: "", //重量
        goods_cat: "", //上传的参数
        pics: [],
        attrs: [],
      },

      deliverCatForm: {
        cat_pid: "", //分类ID
        cat_name: "",
        cat_level: "2", //层级为2级
        attr_id:''
      },
      // 
      deliverCatFormFn:{
          attr_id	:'',
          attr_name	:'',
          cat_id:'',
          attr_sel:'',
          attr_write:'',	
          attr_vals:''
      },

      options:{
          value: 'cat_id',
          label: 'cat_name',
          children:"children"
      },

      dongList:[],//动态数组 商品分类
      jingList:[],//静态数组 商品分类

      // Image:



    });
    

    //定义方法
    //获取商品分类
    let getCategoriesFn=()=>{
      getCategories().then((res) => {
          addObj.list=res
          console.log(addObj.list);
      });
    }
    getCategoriesFn()//直接调用

    let handleChange=(value)=>{
      addObj.deliverCatForm.cat_pid=value[value.length-1]
    }

    let deliverAppDialogFn=async ()=>{
      getCategories({type:3}).then(res=>{
        addObj.list=res
      })
    }

    //获取动态参数
    let getActiveListFn1=async ()=>{
        // console.log(addObj.value);
        // console.log(addObj.deliverCatFormFn.attr_id);
        let res=await getActiveList(addObj.cateId,"many")
        console.log(res);
        res.map(item=>{
          item.attr_vals=item.attr_vals.split(',')
        })
        
        addObj.dongList=res
        console.log(addObj.dongList);
    }
    //获取静态参数
    let getActiveListFn2=async ()=>{
      let res=await getActiveList(addObj.cateId,"only")
      console.log(res);
      addObj.jingList=res
    }

    let Changepage=()=>{

      if(addObj.value.length!=3 && addObj.active==1){
        ElMessage({
          type:'error',
          message:"警告！必须是三级分类",
          showClose:true
        })
      }else if(addObj.value.length!=3 && addObj.active==2){
        ElMessage({
          type:'error',
          message:"警告！必须是三级分类",
          showClose:true
        })
      }

      if(addObj.active==1){//判断
        if(addObj.cateId){
          getActiveListFn1()
        }
      }else if(addObj.active==2){
        if(addObj.cateId){
          getActiveListFn2()
        }
      }
    }

    //添加商品
    let edieAdd=async ()=>{
      addObj.editAppInfoFrom.goods_cat=addObj.value.toString();
    
      addObj.dongList.map(item=>{//将修改的动态数组添加到attrs
        const dd={
          attr_id:item.attr_id,
          attr_vals:item.attr_vals.join(',')
        }
        addObj.editAppInfoFrom.attrs.push(dd)
      })
      addObj.jingList.map(item=>{
        const jj={
          attr_id:item.attr_id,
          attr_vals:item.attr_vals
        }
        addObj.editAppInfoFrom.attrs.push(jj)
      })

      await addGoods(addObj.editAppInfoFrom)
      router.push({
        name:'goods'
      })

    }

    addObj.cateId=computed(()=>{//计算属性
        if(addObj.value.length==3){
          return addObj.value[2]
        }
        return null;
      })

    //返出去
    return {
      addObj,
      getCategoriesFn,
      handleChange,
      deliverAppDialogFn,

      getActiveListFn1,

      Changepage,

      edieAdd
    };
  },
};
</script>
<style lang="scss" scoped>
.step {
  margin: 10px 0 0 0;
}
.inputk{
  width: 100px;
}
</style>
